package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ClassName: RoleController
 * Description:
 * date: 2019/12/2 16:32
 * Author Airson
 * Since JDK 1.8
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.pageQuery(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> map) {
        try {
            //json数组的转换
            JSONArray menuIdsjsonArray = (JSONArray) map.get("menuIds");
            Integer[] menuIds = menuIdsjsonArray.toArray(new Integer[]{});
            //System.out.println(Arrays.toString(menuIds));

            JSONArray permissionsjsonArray = (JSONArray) map.get("permissions");
            Integer[] permissions = permissionsjsonArray.toArray(new Integer[]{});
            //System.out.println(Arrays.toString(menuIds));
            //Json对象的转换
            JSONObject jsonObject = (JSONObject) map.get("formData");
            Role role = jsonObject.toJavaObject(Role.class);
            //System.out.println(role);

            roleService.add(role, menuIds, permissions);
            return new Result(true, "添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer id) {
        try {
            roleService.delById(id);
            return new Result(true, "删除角色成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return  new Result(true,"查询角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    @RequestMapping("/findmenuIdsByRoleId")
    public Result findmenuIdsByRoleId(Integer id) {
        try {
            List<Integer> menuidList = roleService.findmenuIdsById(id);
            return new Result(true, "查询菜单成功", menuidList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

    @RequestMapping("/findPermissionIdsById")
    public Result findPermissionIdsById(Integer id) {
        try {
            List<Integer> PermissionIds = roleService.findPermissionIdsById(id);
            return new Result(true, "查询菜单成功", PermissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String, Object> map) {
        try {
            //json数组的转换
            JSONArray menuIdsjsonArray = (JSONArray) map.get("menuIds");
            Integer[] menuIds = menuIdsjsonArray.toArray(new Integer[]{});
            //System.out.println(Arrays.toString(menuIds));

            JSONArray permissionsjsonArray = (JSONArray) map.get("permissions");
            Integer[] permissions = permissionsjsonArray.toArray(new Integer[]{});
            //System.out.println(Arrays.toString(menuIds));
            //Json对象的转换
            JSONObject jsonObject = (JSONObject) map.get("formData");
            Role role = jsonObject.toJavaObject(Role.class);
            //System.out.println(role);

            roleService.edit(role, menuIds, permissions);
            return new Result(true, "更新角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新角色失败");
        }
    }
}
